/**
 * This class prepares and returns overall subject analysis pivot table
 */
package slhs.tempo.pivotTableUtilities;

import java.awt.Color;
import java.awt.Dimension;
import java.text.NumberFormat;
import java.util.Hashtable;
import java.util.Properties;

import javax.swing.table.TableModel;

import org.openswing.swing.domains.java.Domain;
import org.openswing.swing.internationalization.java.EnglishOnlyResourceFactory;
import org.openswing.swing.message.receive.java.Response;
import org.openswing.swing.pivottable.client.PivotTable;
import org.openswing.swing.pivottable.client.PivotTableController;
import org.openswing.swing.pivottable.functions.java.AverageFunction;
import org.openswing.swing.pivottable.functions.java.GenericFunction;
import org.openswing.swing.pivottable.java.ColumnField;
import org.openswing.swing.pivottable.java.DataField;
import org.openswing.swing.pivottable.java.PivotTableParameters;
import org.openswing.swing.pivottable.java.RowField;
import org.openswing.swing.pivottable.server.PivotTableEngine;
import org.openswing.swing.pivottable.tablemodelreaders.server.TableModelReader;
import org.openswing.swing.table.profiles.client.FileGridProfileManager;
import org.openswing.swing.util.client.ClientSettings;

import slhs.tempo.scriptGenerator.UtilityConstants;

/**
 * <h1>Overall Subject Analysis Pivot Table</h1>
 * <p>
 * This class creates and returns overall subject analysis pivot table
 * </p>
 * 
 * @author Swapnil Devikar
 * 
 */
public class ProbesAnalysisPivotTable {

    TableModel formattedTableModel;
    private PivotTable pivotTable;
    private boolean isOverallAnalysis;

    public ProbesAnalysisPivotTable(TableModel formattedTableModel, boolean isOverallAnalysis) {
	this.formattedTableModel = formattedTableModel;
	this.pivotTable = new PivotTable();
	this.isOverallAnalysis = isOverallAnalysis;

	Hashtable domains = new Hashtable();
	Properties props = new Properties();
	props.setProperty("Subject", "Subject");
	props.setProperty("Reaction Time", "Reaction Time");
	props.setProperty("Reaction Count", "Reaction Count");
	props.setProperty("Mean Reaction Time", "Mean Reaction Time");
	props.setProperty("Control Mean Raaction Time", "Control Mean Reaction Time");
	props.setProperty("Related Mean Raaction Time", "Related Mean Reaction Time");
	props.setProperty("Type", "Ctl/Rel");
	props.setProperty("Item Number", "Probe Point");
	props.setProperty("Probe", "Probe");
	props.setProperty("Standard Deviation", "Standard Deviation");
	props.setProperty("All Probe Points", "All Probe Points");

	ClientSettings clientSettings = new ClientSettings(new EnglishOnlyResourceFactory("$", props, true), domains);
	ClientSettings.FILTER_PANEL_ON_GRID = true;
	Domain orderStateDomain = new Domain("PROBES_ANALYSIS");
	orderStateDomain.addDomainPair(new Integer(0), "opened");
	orderStateDomain.addDomainPair(new Integer(1), "suspended");
	orderStateDomain.addDomainPair(new Integer(2), "delivered");
	orderStateDomain.addDomainPair(new Integer(3), "closed");
	domains.put(orderStateDomain.getDomainId(), orderStateDomain);
	ClientSettings.ALLOW_OR_OPERATOR = false;
	ClientSettings.INCLUDE_IN_OPERATOR = false;
	ClientSettings.GRID_PROFILE_MANAGER = new FileGridProfileManager();

	configurePivotTable();

    }

    /**
     * Create pivot table
     */
    private void configurePivotTable() {
	TableModelReader reader = new TableModelReader(formattedTableModel);
	final PivotTableEngine engine = new PivotTableEngine(reader);

	pivotTable.setController(new PivotTableController() {

	    @Override
	    public Response getPivotTableModel(PivotTableParameters pars) {
		long t = System.currentTimeMillis();
		Response res = engine.getPivotTableModel(pars);
		return res;
	    }

	});

	String itemsRowCategory = "Probe";

	RowField rowFieldCategory = new RowField(itemsRowCategory, UtilityConstants.getScreenWidth() / 8);
	RowField rowFieldAggregationCategory = new RowField(itemsRowCategory, UtilityConstants.getScreenWidth() / 8,
		new CategoryAggregator("All Probes"));

	// apply category to table
	pivotTable.getAllRowFields().add(rowFieldAggregationCategory);
	pivotTable.addRowField(rowFieldAggregationCategory);
	pivotTable.getAllRowFields().add(rowFieldCategory);
	pivotTable.addRowField(rowFieldCategory);

	String probePointAggregation = "Item Number";
	String probePointCategory = "Item Number";
	String probeTypeCategory = "Ctl/Rel";

	ColumnField columnFieldProbeAggregation = new ColumnField(probePointAggregation, "All Probe Points",
		new CategoryAggregator("All Probe Points"));
	ColumnField columnFieldProbe = new ColumnField(probePointCategory, "Mean Reaction Time");
	ColumnField columnFieldProbeType = new ColumnField(probeTypeCategory, "Type");

	// apply category to all column fields in table
	pivotTable.getAllColumnFields().add(columnFieldProbeAggregation);
	pivotTable.getAllColumnFields().add(columnFieldProbe);
	pivotTable.getAllColumnFields().add(columnFieldProbeType);

	pivotTable.addColumnField(columnFieldProbeAggregation);
	pivotTable.addColumnField(columnFieldProbe);
	pivotTable.addColumnField(columnFieldProbeType);

	String reactionTimes = "Reaction Time";
	DataField dataFieldReactionCount = new DataField(reactionTimes, UtilityConstants.PIVOT_TABLE_DATA_FIELD_SIZE,
		"Reaction Count", new GenericFunction());
	DataField dataFieldResponseTime = new DataField(reactionTimes, UtilityConstants.PIVOT_TABLE_DATA_FIELD_SIZE,
		"Mean Reaction Time", new AverageFunction());
	DataField dataFieldStandardDeviation = new DataField(reactionTimes,
		UtilityConstants.PIVOT_TABLE_DATA_FIELD_SIZE, "Standard Deviation", new StandardDeviationFunction());

	NumberFormat nf = NumberFormat.getIntegerInstance();
	nf.setMaximumFractionDigits(2);
	nf.setMinimumFractionDigits(2);

	dataFieldReactionCount.setFormatter(nf);
	dataFieldResponseTime.setFormatter(nf);
	dataFieldResponseTime.setFormatter(nf);

	pivotTable.getAllDataFields().add(dataFieldReactionCount);
	pivotTable.getAllDataFields().add(dataFieldResponseTime);
	pivotTable.getAllDataFields().add(dataFieldStandardDeviation);

	pivotTable.addDataField(dataFieldReactionCount);
	pivotTable.addDataField(dataFieldResponseTime);
	pivotTable.addDataField(dataFieldStandardDeviation);

	Dimension size = new Dimension((UtilityConstants.getScreenWidth()), (UtilityConstants.getScreenHeight() / 2));
	pivotTable.setPreferredSize(size);
	pivotTable.setBackground(Color.GRAY);
	pivotTable.setForeground(Color.RED);

    }

    /**
     * @return
     */
    public PivotTable get() {
	// TODO Auto-generated method stub
	return pivotTable;
    }

}
